package com.zidian.teacher.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.teacher.R;
import com.zidian.teacher.adapter.StudyResultAdapter;
import com.zidian.teacher.bean.StudentResultBean;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/9/8 0008.
 */
public class StudentResultActivity extends ToolBarActivity {

    @Bind(R.id.result_recycler)
    RecyclerView resultRecycler;
    @Bind(R.id.swipe_refresh_widget)
    SwipeRefreshLayout mSwipeRefreshWidget;

    boolean isLoading;
    private List<StudentResultBean.DataBean> data = new ArrayList<>();
    private StudyResultAdapter adapter;

    private boolean isRefreshing = false;
    private String courseId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_teaching_student_result);
        ButterKnife.bind(this);
        setTitle("全部学生成绩");
        courseId = getIntent().getStringExtra("courseId");
        adapter = new StudyResultAdapter(this, data);
        initView();
        initData();

    }

    public void initView() {

        mSwipeRefreshWidget.setColorSchemeResources(R.color.green);

        mSwipeRefreshWidget.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshWidget.setRefreshing(true);
                isRefreshing = true;
            }
        });
        mSwipeRefreshWidget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                data.clear();
                isRefreshing = true;
                getStudentResult();
            }
        });

        resultRecycler.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isRefreshing) {
                    return true;
                } else {
                    return false;
                }
            }
        });

        final GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (adapter.isBottomView(position)) ? layoutManager.getSpanCount() : 1;
            }
        });
        resultRecycler.setLayoutManager(layoutManager);
        resultRecycler.setAdapter(adapter);


        //添加点击事件
        adapter.setOnItemClickListener(new StudyResultAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.d("test", "item position = " + position);
//                startActivity(InteractionAskDetailAct.class);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

    }

    public void initData() {
        getStudentResult();
    }

    /**
     * 获取测试数据
     */
    private void getData(List<StudentResultBean.DataBean> model) {


        data.addAll(model);

        adapter.notifyDataSetChanged();
        mSwipeRefreshWidget.setRefreshing(false);
        adapter.notifyItemRemoved(adapter.getItemCount());
    }

    private void getStudentResult() {

        String loginUrl = Constant.URL_GOAL + "grade/select";
        HashMap<String, String> map = new HashMap<>();
        map.put("courseId", courseId);
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("token", GetUserInfo.getToken(context));


        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mSwipeRefreshWidget.setRefreshing(false);
                ToastUtil.showToast(context, "获取列表失败！");
                isRefreshing = false;
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                mSwipeRefreshWidget.setRefreshing(false);
                isRefreshing = false;
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {
                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        isLoading = true;
                        adapter.setMore(false);
                        return;
                    } else {
                        Gson gson = new Gson();
                        StudentResultBean model = gson.fromJson(response, StudentResultBean.class);

                        isLoading = true;
                        adapter.setMore(false);

                        if (model.isSuccess()) {
                            getData(model.getData());
                        }
                    }

                } catch (JSONException e) {

                }



            }
        });
    }


}
